// 北京时间：2021-11-18 22:14:24
/* 时间yyyy-MM-dd HH:mm:ss转为时间戳 */
function timeToTimestamp(time) {
    let timestamp = Date.parse(new Date(time).toString());
    //timestamp = timestamp / 1000; //时间戳为13位需除1000，时间戳为13位的话不需除1000
    console.log(time + "的时间戳为：" + timestamp);
    return timestamp;
    //2021-11-18 22:14:24的时间戳为：1637244864707
}

function getRandInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


// 时间戳：1637244864707
/* 时间戳转换为时间 */
function timestampToTime(timestamp) {
    timestamp = timestamp ? timestamp : null;
    let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
}

function initPages(total) {
    let pages = Math.ceil(total / 30 / 20);
    let showPages = [];

    for (let i = pages - 3; i < pages; i++) {
        showPages.push(`<button type="button" class="next-btn next-medium next-btn-normal next-pagination-item" data-spm-anchor-id="a21ffj.benefit-template-detail.0.i9.46fc7f1bJEpeAx"><span class="next-btn-helper">${i}</span></button>`);
    }

    let pageHtml = `<button aria-label="上一页，当前第${pages}页" type="button" class="next-btn next-medium next-btn-normal next-pagination-item next-prev"><i class="next-icon next-icon-arrow-left next-xs next-btn-icon next-icon-first next-pagination-icon-prev"></i><span class="next-btn-helper">上一页</span></button><div class="next-pagination-list"><button aria-label="第1页，共17页" type="button" class="next-btn next-medium next-btn-normal next-pagination-item"><span class="next-btn-helper">1</span></button><i class="next-icon next-icon-ellipsis next-medium next-pagination-ellipsis next-pagination-icon-ellipsis"></i>${showPages.join('')}<button aria-label="第17页，共17页" type="button" class="next-btn next-medium next-btn-normal next-pagination-item next-current" data-spm-anchor-id="a21ffj.benefit-template-detail.0.i10.46fc7f1bJEpeAx"><span class="next-btn-helper">${pages}</span></button></div><button type="button" disabled="" class="next-btn next-medium next-btn-normal next-pagination-item next-next"><span class="next-btn-helper">下一页</span><i class="next-icon next-icon-arrow-right next-xs next-btn-icon next-icon-last next-pagination-icon-next"></i></button><span class="next-pagination-display"><em>${pages}</em>/${pages}</span><span class="next-pagination-jump-text">到第</span><span class="next-input next-medium next-pagination-jump-input"><input aria-label="请输入跳转到第几页" height="100%" autocomplete="off" value=""></span><span class="next-pagination-jump-text">页</span><button type="button" class="next-btn next-medium next-btn-normal next-pagination-jump-go"><span class="next-btn-helper">确定</span></button>`;
    document.querySelector('div.next-pagination-pages').innerHTML = pageHtml;
}

function initRecords(username, datetime) {
    let recordHtml = [];
    let filterUsername = username.substr(0, 1) + '**';

    for (let i = 0; i < 20; i++) {
        {
            let rowClass = '';

            if (i == 0) {
                rowClass = 'first';
            }

            if (i == 20 - 1) {
                rowClass = 'last';
            }

            let timeStamp = timeToTimestamp(datetime) - 2000;
            datetime = timestampToTime(timeStamp);
            recordHtml.push(`<tr class="next-table-row ${rowClass}" role="row"><td data-next-table-col="0" data-next-table-row="0" class="next-table-cell first" role="gridcell" style="text-align: left;"><div class="next-table-cell-wrapper" data-next-table-row="0"><span>${filterUsername}</span></div></td><td data-next-table-col="1" data-next-table-row="0" class="next-table-cell" role="gridcell" style="text-align: left;"><div class="next-table-cell-wrapper" data-next-table-row="0"><span>30个</span></div></td><td data-next-table-col="2" data-next-table-row="0" class="next-table-cell" role="gridcell" style="text-align: left;"><div class="next-table-cell-wrapper" data-next-table-row="0"><span data-spm-anchor-id="a21ffj.benefit-template-detail.0.i2.46fc7f1bomEq9Q">${datetime}</span></div></td><td data-next-table-col="3" data-next-table-row="0" class="next-table-cell" role="gridcell" style="text-align: left;"><div class="next-table-cell-wrapper" data-next-table-row="0"><span>权益直发</span></div></td><td data-next-table-col="4" data-next-table-row="0" class="next-table-cell last" role="gridcell" style="text-align: left;"><div class="next-table-cell-wrapper" data-next-table-row="0"><span>账号直发</span></div></td></tr>`)
        }
    }

    document.querySelector('tbody.next-table-body').innerHTML = recordHtml.join('');
}

function initOther(userName) {
    document.querySelector('input[placeholder="请输入淘宝账号"]').value = userName;
    let spans = $x('//div[contains(@class,"BenefitDetailDataCard--itemNum--")]/child::span');
    let quantity = 5000000;
    let stock = getRandInteger(2000000, 4000000);
    spans[0].innerText = quantity;
    spans[1].innerText = stock;
    spans[3].innerText = quantity * 30;
}

function initData(username, datetime, total) {
    initRecords(username, datetime);
    initPages(total);
    initOther(username);
}

initData('tb8805098551', '2024-01-17 22:50:36', 10000);